package com.zj.study.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zj.study.entity.User;
import com.zj.study.pojo.FinalPageVO2;
import com.zj.study.pojo.FinalResultVO;
import com.zj.study.pojo.dto.UserDTO;
import com.zj.study.pojo.query.UserQuery;
import com.zj.study.pojo.vo.UserVO;
import com.zj.study.service.UserService;
import com.zj.study.utils.beancopier.BeanCopier;
import com.zj.study.utils.validation.annotation.Valid;
import com.zj.study.utils.validation.core.ConstraintValidationAspect;
import com.zj.study.utils.validation.validator.AbstractValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author 赵赳
 * @since 2021/9/17 17:50
 */
@RestController
public class UserController {

  private UserService userService;

  @Autowired
  public void setUserService(UserService userService) {
    this.userService = userService;
  }

  /**
   * 查询分页
   *
   * @return 分页数据
   */
  @GetMapping("query")
  public FinalPageVO2<User> queryOneOrList() {
    return userService.queryOneOrList();
  }

  /**
   * 条件查询
   */
  @PostMapping("/queryUser")
  public FinalResultVO<List<UserVO>> queryUser(@RequestBody(required = false) UserQuery query) {

    return FinalResultVO.ok(
        Optional.ofNullable(userService.queryUser(query))
            .orElse(List.of())
            .stream()
            .map(u -> BeanCopier.copy(u, UserVO.class))
            .collect(Collectors.toList())
    );
  }

  /**
   * 更新用户信息 测试 @Valid 注解 Valid 实现
   *
   * @return 结果
   * @see ConstraintValidationAspect --> {@link AbstractValidator}
   */
  @PostMapping("/update")
  public FinalResultVO<Void> updateUser(@RequestBody @Valid UserDTO dto) {
    var userModel = BeanCopier.copy(dto, User.class)
        .setUpdateTime(LocalDateTime.now());
    return userService.updateById(userModel) ? FinalResultVO.ok() : FinalResultVO.fail("修改用户信息失败");
  }

  /**
   * @return 用户列表
   */
  @GetMapping("/user")
  public FinalResultVO<List<User>> getUserList() {
    return FinalResultVO.ok(userService.lambdaQuery().list());
  }

   /**
    * 用户详情
    *
    * @return 用户详情信息
   */
   @GetMapping("/user/{id}")
   public FinalResultVO<UserVO> getDetail(@PathVariable @NonNull Long id) {
        var user = userService.lambdaQuery().eq(User::getId, id).one();
        if (Objects.isNull(user)) {
            return FinalResultVO.fail("用户不存在");
        }
        return FinalResultVO.ok(BeanCopier.copy(user, UserVO.class));
   }

  /**
   * 新增用户
   *
   * @return 结果
   */
  @PostMapping("/save")
  public FinalResultVO<Void> saveUser(@RequestBody @NonNull User user) {
    return userService.save(user) ? FinalResultVO.ok() : FinalResultVO.fail("新增用户失败");
  }

  /**
   * 分页查询
   */
  @GetMapping("/page/{pageNum}/{pageSize}")
  public FinalPageVO2<User> getPage(@PathVariable Long pageNum, @PathVariable Long pageSize) {
    var page = userService.page(new Page<User>().setCurrent(pageNum).setSize(pageSize));
    return FinalPageVO2.ok(page);
  }


}
